package data.composition.factory.impl;

import cn.hutool.core.lang.func.Func1;
import data.composition.factory.core.CollectionMapping;
import data.composition.factory.core.CollectionMappingInner;
import data.composition.factory.core.DataComposition;
import data.composition.factory.core.MapObj;
import lombok.Getter;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Function;

/**
 * @author ZhangJinyu
 * @since 2024-12-13
 */
@Getter
public class CollectionMappingImpl<T, R, V> implements CollectionMappingInner<T, R, V> {
    private final List<MapObj> mapObjs;
    private final DataComposition<T> dataComposition;
    private final Function<T, Collection<V>> dk;

    public CollectionMappingImpl(DataComposition<T> dataComposition, Function<T, Collection<V>> dk) {
        this.dataComposition = dataComposition;
        this.dk = dk;
        mapObjs = new LinkedList<>();
    }

    @Override
    public List<MapObj> getMapObjs() {
        return mapObjs;
    }

    @Override
    public Function<T, Collection<V>> getDataFunction() {
        return dk;
    }

    @Override
    public CollectionMapping<T, R, V> map(Func1<T, Collection<R>> left) {
        mapObjs.add(new MapObj(left, o -> o));
        return this;
    }

    @Override
    public <RV> CollectionMapping<T, R, V> map(Func1<T, Collection<RV>> left, Function<R, RV> right) {
        mapObjs.add(new MapObj(left, right));
        return this;
    }

    @Override
    public DataComposition<T> over() {
        return dataComposition;
    }
}
